﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace FACUI.frmDialog
{
    public partial class frmStart : Form
    {
        void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;            
            try
            {
                //接收传感器组数据  端口10002
                BasicClass.GetData getData = new BasicClass.GetData(10002);
                Thread th = new Thread(new ThreadStart(getData.ReceiveData));
                th.IsBackground = true;
                th.Start();
                //接收原始光谱数据 端口8001
                //GetSource  _udpSource = new GetSource(8001);
                //Thread thsource = new Thread(new ThreadStart(_udpSource.ReceiveData));
                //thsource.IsBackground = true;
                //thsource.Start();
                //接收转发到9091转发的数据
                GetSource _udpSource = new GetSource(9091);
                Thread thsource = new Thread(new ThreadStart(_udpSource.ReceiveData));
                thsource.IsBackground = true;
                thsource.Start();
                //继电器控制板通信
                CSource.thMcuSendToCore = new Thread(CSource.tdtc.SendHeart);
                CSource.thMcuSendToCore.Start();
                CSource.thMcuSendToCore.IsBackground = true;
                if (txtUser.Text != string.Empty)
                {
                    btnOK_Click(null, new EventArgs());
                }
            }
            catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.StackTrace+":"+ex.Message ); }
        }

        public frmStart(string strUser)
        {
            InitializeComponent();
            if (strUser != string.Empty)
            {
                this.txtUser.Text = strUser;
                btnOK.Click += new EventHandler(btnOK_Click);
            }
            timer1.Interval = 100;
            timer1.Enabled = true;
            timer1.Tick += new EventHandler(timer1_Tick);                
        }
        private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
        void btnOK_Click(object sender, EventArgs e)
        {
            frmMain _frmMain = new frmMain();
            //_frmMain.Text = lblTitle.Text +string.Empty.PadLeft(16,' ')  + txtUser.Text;
            this.Hide();
            _frmMain.Show();
        }

   
        private void btnCancle_Click(object sender, EventArgs e)
        {
            try
            {
                Application.Exit();
            }
            catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.TargetSite+ex.Message ); }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            btnOK.Focus();
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                btnCancle_Click(null,null);
            }
            else if (keyData == Keys.Enter)
            {
                btnOK_Click(null,null);
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
